<template>
  <div class="refund">
    <el-dialog
      title="退货"
      destroy-on-close
      :visible.sync="dialogVisible"
      width="45%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <div style="margin: 0 20px">
          <p>注：1.已用优惠券不予退还</p>
          <p>2.退款后积分将按照退款金额进行扣减</p>
          <p>3.退款金额将在1-3个工作日内退还至支付账户</p>
        </div>
        <el-form-item label="退货原因：" prop="refundReason">
          <el-radio-group v-model="ruleForm.refundReason">
            <el-radio label="00">下错单</el-radio>
            <el-radio label="01">不想要了</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退货说明：" prop="refundExplain">
          <el-input
            v-model="ruleForm.refundExplain"
            placeholder="请填写退货说明"
            maxlength="256"
            show-word-limit
            :autosize="{ minRows: 4, maxRows: 6 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <div style="padding: 20px 40px" v-if="paySerials.length">
          退款信息
          <div>
            <p>退款金额:￥{{ refundAmt }}</p>
            <p>
              退回账户:<span v-for="(item, index) in paySerials" :key="index"
                >{{ item.payChannelNo | status
                }}<span v-if="paySerials.length - 1 != index">, </span></span
              >
            </p>
          </div>
        </div>
        <el-form-item label="审核意见：" prop="auditOpinion">
          <el-input
            v-model="ruleForm.auditOpinion"
            placeholder="请填写审核意见"
            maxlength="256"
            show-word-limit
            :autosize="{ minRows: 4, maxRows: 6 }"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handlSure" :loading="loadingSure"
          >同意退货，交易关闭</el-button
        >
        <el-button @click="noSure" :loading="loadingNoSure"
          >不同意退货</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { appApplyRefund, rejectRefund } from "@/api/shop/workorder/refund.js";
export default {
  filters: {
    status(val) {
      let obj = "";
      switch (val) {
        case "00":
          obj = "支付宝";
          break;
        case "01":
          obj = "微信";
          break;
        case "02":
          obj = "储值卡";
          break;
      }
      return obj;
    },
  },
  data() {
    return {
      //退货
      dialogVisible: false,
      ruleForm: {},
      paySerials: [],
      loadingSure: false,
      loadingNoSure: false,
      rules: {
        refundExplain: [
          {
            required: true,
            message: "请填写退货说明",
            trigger: "blur",
          },
        ],
        auditOpinion: [
          {
            required: true,
            message: "请填写审核意见",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    show(obj) {
      this.dialogVisible = true;
      if (obj.paySerials) {
        //退款信息
        this.paySerials = obj.paySerials;
      }
      this.ruleForm = {
        refundReason: obj.refundReason,
        subOrderId: obj.subOrderId,
        refundExplain: obj.refundExplain,
      };
    },
    handlSure() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.loadingSure = true;
          appApplyRefund(this.ruleForm)
            .then((val) => {
              this.$message.success("退款成功");
              this.close();
            })
            .catch(() => {
              this.close();
            });
        } else {
          return false;
        }
      });
    },
    noSure() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.loadingNoSure = true;
          rejectRefund(this.ruleForm)
            .then((val) => {
              this.close();
            })
            .catch(() => {
              this.close();
            });
        } else {
          return false;
        }
      });
    },
    close() {
      this.loadingNoSure = false;
      this.loadingSure = false;
      this.dialogVisible = false;
      this.$parent.triggerFn();
    },
  },
};
</script>

<style lang="scss">
.refund {
  .el-dialog__body {
    padding: 0px 20px;
  }
}
</style>